extends Control

@export var normal_scale: Vector2 = Vector2.ONE
@export var select_scale: Vector2 = Vector2(1.5, 1.5)
var item_data: ItemData
@onready var texture_rect: TextureRect = $Item/TextureRect
@onready var count_label: Label = $Item/Count
@onready var name_label: Label = $Item/Control/Name


func init(item_data: ItemData):
	if not texture_rect: texture_rect = $Item/TextureRect
	
	self.item_data = item_data
	
	var texture = ResourceManager.get_resource(self.item_data.item_config.icon_name)
	texture_rect.texture = texture
	
	if not name_label: name_label = $Item/Control/Name
	name_label.text = self.item_data.item_config.name
	
	if not count_label: count_label = $Item/Count
	if self.item_data.item_config.stackable:
		count_label.visible = true
		count_label.text = "x" + str(self.item_data.item_count)
	else:
		count_label.visible = false


func select():
	if scale == select_scale: return
	
	var tween = create_tween()
	tween.tween_property(self, "scale", select_scale, 0.2) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_OUT)
	

func unselect():
	if scale == normal_scale: return
	
	var tween = create_tween()
	tween.tween_property(self, "scale", normal_scale, 0.2) \
	.set_trans(Tween.TRANS_SINE) \
	.set_ease(Tween.EASE_OUT)
